let App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    isLoading: true, // 是否正在加载中
    page: 1, // 当前页码
    scrollHeight: 0,
    list: [],
    onmore: true,
    search_num: '',
    dataList: [],
    sercherStorage: [],
    StorageFlag: false, //显示搜索记录标志位
    is_display: 'none',
    show: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setListHeight();

  },
  getInput: function (e) {
    this.setData({
      search_num: e.detail.value
    })
    if (this.data.search_num != '') {
      this.setData({
        is_display: ''
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  //生命周期函数--监听页面初次渲染完成
  onReady: function (e) {
    var that = this;
    //请求数据

  },

  /**
   * 下拉到底加载数据
   */
  onDownLoad() {

  },

  nono: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      onmore: true,

    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getInfo() {

    let _this = this;
    if (_this.data.search_num == '' || _this.data.search_num.trim() == '') {
      wx.showToast({
        title: '请输入单号',
        icon: 'error'
      })
      return false;
    }
    wx.showLoading();
    //控制搜索历史
    if (_this.data.search_num != '' && _this.data.sercherStorage.indexOf(_this.data.search_num) < 0) {
      //将搜索记录更新到缓存
      var postageData = _this.data.sercherStorage;
      postageData.push(
        _this.data.search_num
      )
      wx.setStorageSync('postageData', postageData);
      _this.setData({
        StorageFlag: false,
      })
    }
    App._get('postage', {
      number: _this.data.search_num
    }, result => {
      if (result.code == 0) {

        if (result.data.list.success) {
          _this.setData({
            name: result.data.companyName,
            dataList: result.data.list.data
          })
        } else {
          App.showError('请检查单号是否错误')
        }

      } else {
        _this.setData({
          name: "",
          dataList: []
        })
        App.showSuccess("暂无信息!")
      }
    }, function () {}, function () {

      wx.hideLoading();
    });

  },

  /**
   * 设置分类列表高度
   */
  setListHeight() {
    let _this = this;
    wx.getSystemInfo({
      success(res) {
        _this.setData({
          scrollHeight: res.windowHeight - 47,
        });
      }
    });
  },
  /**
   * 删除用户输入
   */
  emptyInput: function () {

    var that = this;
    that.setData({
      search_num: ""
    })
  },
  tapSercherStorage: function (e) {
    var that = this;
    var val = e.currentTarget.dataset.name;
    that.setData({
      search_num: val,
      is_display: "block"
    })
    that.getInfo();
  },
    //清除缓存历史
    clearSearchStorage: function () {
      var that = this;
      wx.showModal({
        title: '提示',
        content: '确定清除搜索历史吗',
        success: function (res) {
          if (res.confirm) {
            wx.removeStorageSync('postageData')
            that.setData({
              sercherStorage: [],
              StorageFlag: false,
            })
          }
  
        }
      });
  
  
    },
    //打开历史记录列表
    openLocationsercher: function () {
      this.setData({
        sercherStorage: wx.getStorageSync('postageData') || [],
        StorageFlag: true,
      })
    },

})